package com.example.demo.service.impl;

import com.example.demo.util.RabbitMQTool;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.stereotype.Service;

import java.util.Map;


@Slf4j
@Service
public class DemoService {

    @Resource
    private RabbitMQTool rabbitMQUtil;

    // 项目启动后自动执行，动态创建并发送消息
    @PostConstruct
    public void init() {
        log.info("【开始动态创建 RabbitMQ 资源并发送测试消息】");

//        // ========== 1. 简单队列 ==========
//        String simpleQueue = "queue.simple.dynamic";
//        rabbitMQUtil.declareQueue(simpleQueue, true);
//        rabbitMQUtil.sendToQueue(simpleQueue, "Hello Simple Queue!");
//
//        // ========== 2. 工作队列 ==========
//        String workQueue = "queue.work.dynamic";
//        rabbitMQUtil.declareQueue(workQueue, true);
//        rabbitMQUtil.sendToQueue(workQueue, "Hello Work Queue Task");
//
//        // ========== 3. Fanout 广播模式 ==========
//        String fanoutExchange = "exchange.fanout.dynamic";
//        String fanoutQueueA = "queue.fanout.a.dynamic";
//        String fanoutQueueB = "queue.fanout.b.dynamic";
//
//        rabbitMQUtil.declareFanoutExchange(fanoutExchange);
//        rabbitMQUtil.declareQueue(fanoutQueueA, true);
//        rabbitMQUtil.declareQueue(fanoutQueueB, true);
//
//        rabbitMQUtil.bindFanout(rabbitMQUtil.declareQueue(fanoutQueueA, true), rabbitMQUtil.declareFanoutExchange(fanoutExchange));
//        rabbitMQUtil.bindFanout(rabbitMQUtil.declareQueue(fanoutQueueB, true), rabbitMQUtil.declareFanoutExchange(fanoutExchange));
//
//        rabbitMQUtil.sendToFanoutExchange(fanoutExchange, "Hello Fanout Broadcast!");
//
//        // ========== 4. Direct 路由模式 ==========
//        String directExchange = "exchange.direct.dynamic";
//        String directQueueRed = "queue.direct.red.dynamic";
//        String directQueueBlue = "queue.direct.blue.dynamic";
//        String routingKeyRed = "red";
//        String routingKeyBlue = "blue";
//
//        rabbitMQUtil.declareDirectExchange(directExchange);
//        rabbitMQUtil.declareQueue(directQueueRed, true);
//        rabbitMQUtil.declareQueue(directQueueBlue, true);
//
//        rabbitMQUtil.bindDirect(rabbitMQUtil.declareQueue(directQueueRed, true), rabbitMQUtil.declareDirectExchange(directExchange), routingKeyRed);
//        rabbitMQUtil.bindDirect(rabbitMQUtil.declareQueue(directQueueBlue, true), rabbitMQUtil.declareDirectExchange(directExchange), routingKeyBlue);
//
//        rabbitMQUtil.sendToDirectExchange(directExchange, routingKeyRed, "Hello Direct Red!");
//        rabbitMQUtil.sendToDirectExchange(directExchange, routingKeyBlue, "Hello Direct Blue!");
//
//        // ========== 5. 延迟队列 路由模式 ==========
//        DirectExchange dlx = rabbitMQUtil.declareDirectExchange("ex.dls.task");
//        Queue realQueue = rabbitMQUtil.declareQueue("queue.task.real", true);
//        rabbitMQUtil.bindDirect(realQueue,dlx,"task.created");
//
//        rabbitMQUtil.declareDelayQueueWithDLX(
//                "queue.task.delay",
//                "ex.dlx.task",
//                "task.created",
//                true,
//                null
//        );
//        rabbitMQUtil.sendToDelayQueue("queue.task.delay", Map.of("id",2),1000000L);
    }
}